<template>
    <div class="updatePwd">
        <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules" v-bind="layout">
            <a-form-model-item has-feedback label="原密码" prop="initialpwd">
                <a-input v-model="ruleForm.initialpwd"  type="password"/>
            </a-form-model-item>
            <a-form-model-item has-feedback label="新密码" prop="pass">
                <a-input v-model="ruleForm.pass" type="password" autocomplete="off" />
            </a-form-model-item>
            <a-form-model-item has-feedback label="确认密码" prop="checkPass">
                <a-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
            </a-form-model-item>
            <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
                <a-button type="primary" @click="submitForm('ruleForm')">提交</a-button>
                <a-button style="margin-left: 10px" @click="resetForm('ruleForm')">重置</a-button>
            </a-form-model-item>
        </a-form-model>
    </div>
</template>
<script>
import {
    cxjglXgmmZj
} from "@/api/login";
export default {
    data() {
        let validatorInitialpwd = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入原密码'))
            } else {
                callback()
            }
        }
        let validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码！'))
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass')
                }
                callback()
            }
        }
        let validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码！'))
            } else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致！"))
            } else {
                callback()
            }
        }
        return {
            ruleForm: {
                pass: '',
                checkPass: '',
                initialpwd: ''
            },
            rules: {
                pass: [{ validator: validatePass, trigger: 'change' }],
                checkPass: [{ validator: validatePass2, trigger: 'change' }],
                initialpwd: [{ validator: validatorInitialpwd, trigger: 'change' }]
            },
            layout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 8 }
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    let self = this;
                    let formData = {
                        password: self.ruleForm.initialpwd,
                        newPassword: self.ruleForm.checkPass
                    }
                  cxjglXgmmZj(formData).then(res=>{
                        if (res.code == 200) {
                            self.$message.success(res.message,10);
                        } else {
                            self.$message.error(res.message,10);

                        }
                    })
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields()
        }
    }
}
</script>
<style>
    
.updatePwd {
    padding: 20px 0;
    background: #fff;
}
</style>
